
function upload_photo(node, _url) {
    $(node).fileupload({
        dataType: 'json',
        url:_url,
        done: function (e, data) {
            if (data.result.ok==0) {
                alert(data.result.msg)
                return false;
            }
            var that = $(node).next('div').find('.pic-box');
            var amount = $(node).data('amount');
            if (amount==0) {
                html=`<div class="m-item"><img src="` + data.result.fileurl + `" alt=""><button type="button" name="button" class="btn btn-default" onclick="return del_photo(this)" data-id="" data-pic="` + data.result.insert_id + `">删除</button></div>`;
                that.html(html);
                pic_value = data.result.insert_id + ',';
                $(node).next('div').find('.add-pic').val(pic_value)
            } else {
                html=`<div class="m-item"><img src="` + data.result.fileurl + `" alt=""><button type="button" name="button" class="btn btn-default" onclick="return del_photo(this)" data-id="" data-pic="` + data.result.insert_id + `">删除</button></div>`;
                that.append(html);
                pic_value = $(node).next('div').find('.add-pic').val() + data.result.insert_id + ',';
                $(node).next('div').find('.add-pic').val(pic_value)
            }

        }
    });
}
function show_photo(node) {
    var _this = $(node);
    val = $.trim(_this.val());
    val_id = $.trim(_this.data('id'));
    var param = {cover:val,cover_id:val_id};
    // if(node == '#teacher_info_pic') {
    //   param.type = 1
    // }
    $.ajax({
      url:"/dboard/upload/show_photo",
      type:"POST",
      data: param,
      success:function(res) {
        res=$.parseJSON(res);
        _this.prev('div').prepend(res);
      }
    })
}

//图片移除操作
function del_photo(e) {
    var _this = $(e);
    var _that = _this.parent().parent().parent().find('input.add-pic');
    var id = _this.data('id');
    var pic = _this.data('pic');
    var upload_arr = _that.val().split(',');
    var arr = '';
    $.each(upload_arr,function(i,j) {
      if(j!=pic && j.length!=0){
        arr += j + ',';
      }
    })
    _that.val(arr);
    var delete_pic = _this.parent().parent().parent().find('input.delete-pic');
    var delete_pic_value = delete_pic.val() + id + ',';
    delete_pic.val(delete_pic_value);
    _this.parent().remove();
}
function upload_file(node, _url) {
    $(node).fileupload({
        dataType: 'json',
        url:_url,
        done: function (e, data) {
            var that = $(node).next('div').find('.pic-box');
            var amount = $(node).data('amount');
            if (amount==0) {
                html=`<div class="m-item"><img src="/static/pdf.png" alt=""><a href="` + data.result.fileurl + `" alt=""></a><button type="button" name="button" class="btn btn-default" onclick="return del_photo(this)" data-id="" data-pic="` + data.result.insert_id + `">删除</button></div>`;
                that.html(html);
                //pic_value = data.result.insert_id + ',';
                pic_value = data.result.insert_id;
                $(node).next('div').find('.add-pic').val(pic_value)
            } else {
                html=`<div class="m-item"><img src="/static/pdf.png" alt=""><a href="` + data.result.fileurl + `" alt=""></a><button type="button" name="button" class="btn btn-default" onclick="return del_photo(this)" data-id="" data-pic="` + data.result.insert_id + `">删除</button></div>`;
                that.append(html);
                //pic_value = $(node).next('div').find('.add-pic').val() + data.result.insert_id + ',';
                pic_value = $(node).next('div').find('.add-pic').val() + data.result.insert_id;
                $(node).next('div').find('.add-pic').val(pic_value)
            }

        }
    });
}
